function register()
{
	
	let username = $("#username").val().trim();
	let password = $("#password").val().trim();
	let gender = $("#gender").val().trim();
	let classname = $("#classname").val().trim();
	if(username == "" || password == "" || gender == "" || classname == "")
	{
		$("#myModalBody").text("请输入完整数据！");
		$("#myModal").modal("show");
		return;
	}
	$.ajax({
		type:"post",
		url:baseUrl + "/register",
		data:
		{
			"username":username,
			"password":password,
			"gender":gender=="男"?"1":"0",
			"classname":classname
		},
		dataType:"json",
		success:function(data)
		{
			if(data["meta"]["status"] == "200")
			{
				$("#myModalBody").text("注册成功！请前往登录");
				$("#myModal").modal("show");
			}
			else 
			{
				$("#myModalBody").text("注册失败！");
				$("#myModal").modal("show");
			}
		},
		error:function()
		{
			$("#myModalBody").text("网络错误！");
			$("#myModal").modal("show");
		}
		
	});
	
	
	
}

function registerPage()
{
	let txt = `
		<div class="container">
			<div class="form-group">
				<label for="username">姓名</label>
				<input id="username" type="text" class="form-control" />
			</div>
			<div class="form-group">
				<label for="password">密码</label>
				<input id="password" type="password" class="form-control" />
			</div>
			<div class="form-group">
				<label >性别</label>
				<select id="gender" class="form-control">
				    <option>男</option>
				    <option>女</option>
				</select>
			</div>
			<div class="form-group">
				<label for="classname">班级</label>
				<input id="classname" type="classname" class="form-control" />
			</div>
			<input type="button" class="btn btn-primary" style="margin: 20px;" value="注册" onclick="register()"/>
		</div>
	`;
	$("body").append(txt);
}


function login()
{
	
	let username = $("#username").val().trim();
	let password = $("#password").val().trim();
	if(username == "" || password == "")
	{
		$("#myModalBody").text("请输入完整数据！");
		$("#myModal").modal("show");
		return;
	}
	$.ajax({
		type:"post",
		url:baseUrl + "/login",
		data:
		{
			"username":username,
			"password":password
		},
		dataType:"json",
		success:function(data)
		{
			if(data["meta"]["status"] == "200")
			{
				document.cookie = "userId=" + data["data"]["userId"];
				window.location.assign("index.html");
			}
			else 
			{
				$("#myModalBody").text("登录失败！账号或者密码错误");
				$("#myModal").modal("show");
			}
		},
		error:function()
		{
			$("#myModalBody").text("网络错误！");
			$("#myModal").modal("show");
		}
		
	});
	
}

function loginOut()
{
	document.cookie = "userId=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
	window.location.reload();
}

function loginPage()
{
	
	let txt = `
		<div class="container">
			<div class="form-group">
				<label for="username">姓名</label>
				<input id="username" type="text" class="form-control" />
			</div>
			<div class="form-group">
				<label for="password">密码</label>
				<input id="password" type="password" class="form-control" />
			</div>
			<input type="button" class="btn btn-primary" style="margin: 20px;" value="登录" onclick="login()"/>
		</div>
	`;
	$("body").append(txt);
	
}


function pageHead()
{
	let txt = "";
	//类型，0为未登录，1为学生，2为老师，3为管理员
	let type = 0;
	if(getCookie("userId") != "")
	{
		$.ajax({
			type:"post",
			url:baseUrl + "/getUserById",
			data:
			{
				"userId":getCookie("userId")
			},
			dataType:"json",
			success:function(data)
			{
				if(data["meta"]["status"] == "200" && data["data"] != "null")
				{
					type = data["data"]["type"];
//					console.log(data);
					if(type == 0)
					{
						txt += `
							<nav class="navbar navbar-inverse">
								<div class="container-fluid">
									<!-- Brand and toggle get grouped for better mobile display -->
									<div class="navbar-header">
										<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
									        <span class="sr-only">Toggle navigation</span>
									        <span class="icon-bar"></span>
									        <span class="icon-bar"></span>
									        <span class="icon-bar"></span>
									    </button>
										<a class="navbar-brand" href="index.html">课题发布&打分系统</a>
									</div>
									<!-- Collect the nav links, forms, and other content for toggling -->
									<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
										<ul class="nav navbar-nav">
											<li ` + ((analyseSearch("title") == "task" || analyseSearch("title") == null) ? "class='active'" : "" ) + `>
												<a href="index.html?title=task">课题</a>
											</li>
										</ul>
										<ul class="nav navbar-nav navbar-right">
											<li ` + (analyseSearch("title") == "login" ? "class='active'" : "" ) + `>
												<a href="index.html?title=login">登录</a>
											</li>
											<li ` + (analyseSearch("title") == "register" ? "class='active'" : "" ) + `>
												<a href="index.html?title=register">注册</a>
											</li>
										</ul>
									</div>
									<!-- /.navbar-collapse -->
								</div>
								<!-- /.container-fluid -->
							</nav>
						`;
					}
					else if(type == 1)
					{
						txt += `
							<nav class="navbar navbar-inverse">
								<div class="container-fluid">
									<!-- Brand and toggle get grouped for better mobile display -->
									<div class="navbar-header">
										<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
									        <span class="sr-only">Toggle navigation</span>
									        <span class="icon-bar"></span>
									        <span class="icon-bar"></span>
									        <span class="icon-bar"></span>
									    </button>
										<a class="navbar-brand" href="index.html">课题发布&打分系统</a>
									</div>
									<!-- Collect the nav links, forms, and other content for toggling -->
									<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
										<ul class="nav navbar-nav">
											<li ` + ((analyseSearch("title") == "task" || analyseSearch("title") == "taskShow" || analyseSearch("title") == null) ? "class='active'" : "" ) + `>
												<a href="index.html?title=task">课题</a>
											</li>
											<li ` + (analyseSearch("title") == "grade" || analyseSearch("title") == "gradeShow" ? "class='active'" : "" ) + `>
												<a href="index.html?title=grade">分数</a>
											</li>
											<li ` + (analyseSearch("title") == "user" ? "class='active'" : "" ) + `>
												<a href="index.html?title=user">个人</a>
											</li>
											
										</ul>
										<ul class="nav navbar-nav navbar-right">
											<li>
												<a href="index.html" onclick="loginOut()">注销</a>
											</li>
										</ul>
									</div>
									<!-- /.navbar-collapse -->
								</div>
								<!-- /.container-fluid -->
							</nav>
						`;
						
					}
					else if(type == 2)
					{
						
						txt += `
							<nav class="navbar navbar-inverse">
								<div class="container-fluid">
									<!-- Brand and toggle get grouped for better mobile display -->
									<div class="navbar-header">
										<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
									        <span class="sr-only">Toggle navigation</span>
									        <span class="icon-bar"></span>
									        <span class="icon-bar"></span>
									        <span class="icon-bar"></span>
									    </button>
										<a class="navbar-brand" href="index.html">课题发布&打分系统</a>
									</div>
									<!-- Collect the nav links, forms, and other content for toggling -->
									<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
										<ul class="nav navbar-nav">
											
											<li ` + ((analyseSearch("title") == "task" || analyseSearch("title") == "taskShow" || analyseSearch("title") == null) ? "class='active'" : "" ) + `>
												<a href="index.html?title=task">课题</a>
											</li>
											<li ` + (analyseSearch("title") == "grade" || analyseSearch("title") == "gradeShow" ? "class='active'" : "" ) + `>
												<a href="index.html?title=grade">分数</a>
											</li>
											<li ` + (analyseSearch("title") == "newTask" ? "class='active'" : "" ) + `>
												<a href="index.html?title=newTask">新课题</a>
											</li>
											<li ` + (analyseSearch("title") == "user" ? "class='active'" : "" ) + `>
												<a href="index.html?title=user">个人</a>
											</li>
											
										</ul>
										<ul class="nav navbar-nav navbar-right">
											<li>
												<a href="index.html" onclick="loginOut()">注销</a>
											</li>
										</ul>
									</div>
									<!-- /.navbar-collapse -->
								</div>
								<!-- /.container-fluid -->
							</nav>
						`;
						
					}
					else if(type == 3)
					{
						txt += `
							<nav class="navbar navbar-inverse">
								<div class="container-fluid">
									<!-- Brand and toggle get grouped for better mobile display -->
									<div class="navbar-header">
										<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
									        <span class="sr-only">Toggle navigation</span>
									        <span class="icon-bar"></span>
									        <span class="icon-bar"></span>
									        <span class="icon-bar"></span>
									    </button>
										<a class="navbar-brand" href="index.html">课题发布&打分系统</a>
									</div>
									<!-- Collect the nav links, forms, and other content for toggling -->
									<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
										<ul class="nav navbar-nav">
											<li ` + ((analyseSearch("title") == "task" || analyseSearch("title") == "taskShow" || analyseSearch("title") == null) ? "class='active'" : "" ) + `>
												<a href="index.html?title=task">课题</a>
											</li>
											<li ` + (analyseSearch("title") == "grade" || analyseSearch("title") == "gradeShow" ? "class='active'" : "" ) + `>
												<a href="index.html?title=grade">分数</a>
											</li>
											<li ` + (analyseSearch("title") == "student" || analyseSearch("title") == "studentShow" ? "class='active'" : "" ) + `>
												<a href="index.html?title=student">学生</a>
											</li>
											<li ` + (analyseSearch("title") == "teacher" || analyseSearch("title") == "teacherShow"? "class='active'" : "" ) + `>
												<a href="index.html?title=teacher">老师</a>
											</li>
											
											<li ` + (analyseSearch("title") == "user" ? "class='active'" : "" ) + `>
												<a href="index.html?title=user">个人</a>
											</li>
										</ul>
										<ul class="nav navbar-nav navbar-right">
											<li>
												<a href="index.html" onclick="loginOut()">注销</a>
											</li>
										</ul>
									</div>
									<!-- /.navbar-collapse -->
								</div>
								<!-- /.container-fluid -->
							</nav>
						`;
						
					}
					$("body").prepend(txt);
				}
			},
			error:function()
			{
				txt += `
					<nav class="navbar navbar-inverse">
						<div class="container-fluid">
							<!-- Brand and toggle get grouped for better mobile display -->
							<div class="navbar-header">
								<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
							        <span class="sr-only">Toggle navigation</span>
							        <span class="icon-bar"></span>
							        <span class="icon-bar"></span>
							        <span class="icon-bar"></span>
							    </button>
								<a class="navbar-brand" href="index.html">课题发布&打分系统</a>
							</div>
							<!-- Collect the nav links, forms, and other content for toggling -->
							<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
								<ul class="nav navbar-nav">
									<li ` + ((analyseSearch("title") == "task" || analyseSearch("title") == null) ? "class='active'" : "" ) + `>
										<a href="index.html?title=task">课题</a>
									</li>
								</ul>
								<ul class="nav navbar-nav navbar-right">
									<li ` + (analyseSearch("title") == "login" ? "class='active'" : "" ) + `>
										<a href="index.html?title=login">登录</a>
									</li>
									<li ` + (analyseSearch("title") == "register" ? "class='active'" : "" ) + `>
										<a href="index.html?title=register">注册</a>
									</li>
								</ul>
							</div>
							<!-- /.navbar-collapse -->
						</div>
						<!-- /.container-fluid -->
					</nav>
				`;
				$("body").prepend(txt);
			}
		});
	}
	else 
	{
		txt += `
			<nav class="navbar navbar-inverse">
				<div class="container-fluid">
					<!-- Brand and toggle get grouped for better mobile display -->
					<div class="navbar-header">
						<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
					        <span class="sr-only">Toggle navigation</span>
					        <span class="icon-bar"></span>
					        <span class="icon-bar"></span>
					        <span class="icon-bar"></span>
					    </button>
						<a class="navbar-brand" href="index.html">课题发布&打分系统</a>
					</div>
					<!-- Collect the nav links, forms, and other content for toggling -->
					<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
						<ul class="nav navbar-nav">
							<li ` + ((analyseSearch("title") == "task" || analyseSearch("title") == null) ? "class='active'" : "" ) + `>
								<a href="index.html?title=task">课题</a>
							</li>
						</ul>
						<ul class="nav navbar-nav navbar-right">
							<li ` + (analyseSearch("title") == "login" ? "class='active'" : "" ) + `>
								<a href="index.html?title=login">登录</a>
							</li>
							<li ` + (analyseSearch("title") == "register" ? "class='active'" : "" ) + `>
								<a href="index.html?title=register">注册</a>
							</li>
						</ul>
					</div>
					<!-- /.navbar-collapse -->
				</div>
				<!-- /.container-fluid -->
			</nav>
		`;
		$("body").prepend(txt);
		
		
	}
	
	
}

function userPage()
{
	let txt = "";
	if(getCookie("userId") == "")
	{
		txt += `
			<div class="container form-group">
				<div class="form-group">
					<label for="username">姓名</label>
					<input id="username" type="text" class="form-control"/>
				</div>
				<div class="form-group">
					<label for="password">密码</label>
					<input id="password" type="password" class="form-control"/>
				</div>
				<div class="form-group">
					<label for="gender">性别</label>
					<select id="gender" class="form-control">
					    <option>男</option>
					    <option>女</option>
					</select>
				</div>
				<div class="form-group">
					<label for="classname">班级</label>
					<input id="classname" type="text" class="form-control"/>
				</div>
				<button class="btn btn-primary">保存</button>
			</div>
		`;
		$("body").append(txt);
		return;
	}
	
	$.ajax({
		type:"post",
		url:baseUrl + "/getUserById",
		data:
		{
			"userId":getCookie("userId")
		},
		dataType:"json",
		success:function(data)
		{
			txt += `
				<div class="container form-group">
					<div class="form-group">
						<label for="username">姓名</label>
						<input id="username" type="text" class="form-control" value="` + data["data"]["username"] + `"/>
					</div>
					<div class="form-group">
						<label for="password">密码</label>
						<input id="password" type="password" class="form-control" value="` + data["data"]["password"] + `"/>
					</div>
					<div class="form-group">
						<label for="gender">性别</label>
						<select id="gender" class="form-control" >
						    <option>男</option>
						    <option ` + (data["data"]["gender"] == "0" ? "selected" : "") + `>女</option>
						</select>
					</div>
					<div class="form-group">
						<label for="classname">班级</label>
						<input id="classname" type="text" class="form-control" value="` + data["data"]["classname"] + `"/>
					</div>
					<button class="btn btn-primary" onclick="saveUser()">保存</button>
				</div>
			`;
			$("body").append(txt);
		},
		error:function()
		{
			txt += `
				<div class="container form-group">
					<div class="form-group">
						<label for="username">姓名</label>
						<input id="username" type="text" class="form-control"/>
					</div>
					<div class="form-group">
						<label for="password">密码</label>
						<input id="password" type="password" class="form-control"/>
					</div>
					<div class="form-group">
						<label for="gender">性别</label>
						<select id="gender" class="form-control">
						    <option>男</option>
						    <option>女</option>
						</select>
					</div>
					<div class="form-group">
						<label for="classname">班级</label>
						<input id="classname" type="text" class="form-control"/>
					</div>
					<button class="btn btn-primary">保存</button>
				</div>
			`;
			$("body").append(txt);
		}
		
	});
	
	
	
}


function saveUser()
{
	
	let username = $("#username").val().trim();
	let password = $("#password").val().trim();
	let gender = $("#gender").val().trim();
	let classname = $("#classname").val().trim();
	if(username == "" || password == "" || gender == "" || classname == "")
	{
		$("#myModalBody").text("请输入完整数据！");
		$("#myModal").modal("show");
		return;
	}
	$.ajax({
		type:"post",
		url:baseUrl + "/updateUserById",
		data:
		{
			"userId":getCookie("userId"),
			"username":username,
			"password":password,
			"gender":gender=="男"?"1":"0",
			"classname":classname
		},
		dataType:"json",
		success:function(data)
		{
			if(data["meta"]["status"] == "200")
			{
				$("#myModalBody").text("保存成功！");
				$("#myModal").modal("show");
			}
			else 
			{
				$("#myModalBody").text("保存失败！");
				$("#myModal").modal("show");
			}
		},
		error:function()
		{
			$("#myModalBody").text("网络错误！");
			$("#myModal").modal("show");
		}
		
	});
	
}

function teacherOrStudentPage()
{
	let type = analyseSearch("title");
	let txt = "";
	$.ajax({
		type:"post",
		url:baseUrl + "/getUserByType",
		dataType:"json",
		data:
		{
			"type":(type == "teacher" ? "2" : "1")
		},
		async:true,
		success:function(data)
		{
			
			if(data["data"]["list"].length == 0)
			{
				txt += `
					<div class="container">
						<table class=" table table-bordered table-striped table-hover text-center">
							<thead>
								<tr>
									<th class="text-center">序号</th>
									<th class="text-center">姓名</th>
									<th class="text-center">操作</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td colspan="3">无数据</td>
								</tr>
							</tbody>
						</table>
					</div>
				`;
			}
			else 
			{
				txt += `
					<div class="container">
						<table class=" table table-bordered table-striped table-hover text-center">
							<thead>
								<tr>
									<th class="text-center">序号</th>
									<th class="text-center">姓名</th>
									<th class="text-center">操作</th>
								</tr>
							</thead>
							<tbody>`;
							for(let i = 0; i < data["data"]["list"].length; i++)
							{
								txt += `
									<tr>
										<td>` + (i + 1) + `</td>
										<td>` + data["data"]["list"][i]["username"] + `</td>
										<td>
											<a href="index.html?title=` + type + `Show&userId=` + data["data"]["list"][i]["userId"] + `"><button class="btn btn-primary btn-xs">查看</button></a>
											<button class="btn btn-danger btn-xs" onclick="deleteTeacherOrStudent(` + data["data"]["list"][i]["userId"] + `)">删除</button>
										</td>
									</tr>
								`;
							}
							txt += `
							</tbody>
						</table>
					</div>
				`;
			}
				
			
			$("body").append(txt);
		},
		error:function()
		{
			txt += `
				<div class="container">
					<table class=" table table-bordered table-striped table-hover text-center">
						<thead>
							<tr>
								<th class="text-center">序号</th>
								<th class="text-center">姓名</th>
								<th class="text-center">操作</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td colspan="3">无数据</td>
							</tr>
						</tbody>
					</table>
				</div>
			`;
			$("body").append(txt);
		}
		
	});
	
	
}

function teacherOrStudentShowPage()
{
	let txt = "";
	$.ajax({
		type:"post",
		url:baseUrl + "/getUserById",
		data:
		{
			"userId":analyseSearch("userId")
		},
		dataType:"json",
		success:function(data)
		{
			txt += `
				<div class="container form-group">
					<div class="form-group">
						<label for="username">姓名</label>
						<input id="username" type="text" class="form-control" value="` + data["data"]["username"] + `"/>
					</div>
					<div class="form-group">
						<label for="password">密码</label>
						<input id="password" type="password" class="form-control" value="` + data["data"]["password"] + `"/>
					</div>
					<div class="form-group">
						<label for="gender">性别</label>
						<select id="gender" class="form-control" >
						    <option>男</option>
						    <option ` + (data["data"]["gender"] == "0" ? "selected" : "") + `>女</option>
						</select>
					</div>
					<div class="form-group">
						<label for="classname">班级</label>
						<input id="classname" type="text" class="form-control" value="` + data["data"]["classname"] + `"/>
					</div>
					<div class="form-group">
						<label for="type">类型</label>
						<select id="type" class="form-control" >
						    <option>学生</option>
						    <option ` + (data["data"]["type"] == "2" ? "selected" : "") + `>老师</option>
						</select>
					</div>
					<button class="btn btn-primary" onclick="saveTeacherOrStudent()">保存</button>
				</div>
			`;
			$("body").append(txt);
		},
		error:function()
		{
			txt += `
				<div class="container form-group">
					<div class="form-group">
						<label for="username">姓名</label>
						<input id="username" type="text" class="form-control"/>
					</div>
					<div class="form-group">
						<label for="password">密码</label>
						<input id="password" type="password" class="form-control"/>
					</div>
					<div class="form-group">
						<label for="gender">性别</label>
						<select id="gender" class="form-control">
						    <option>男</option>
						    <option>女</option>
						</select>
					</div>
					<div class="form-group">
						<label for="classname">班级</label>
						<input id="classname" type="text" class="form-control"/>
					</div>
					<div class="form-group">
						<label for="type">类型</label>
						<select id="type" class="form-control" >
						    <option>学生</option>
						    <option>老师</option>
						</select>
					</div>
					<button class="btn btn-primary">保存</button>
				</div>
			`;
			$("body").append(txt);
		}
		
	});
	
	
}

function saveTeacherOrStudent()
{
	let username = $("#username").val().trim();
	let password = $("#password").val().trim();
	let gender = $("#gender").val().trim();
	let classname = $("#classname").val().trim();
	let type = $("#type").val().trim();
	if(username == "" || password == "" || gender == "" || classname == "" || type == "")
	{
		$("#myModalBody").text("请输入完整数据！");
		$("#myModal").modal("show");
		return;
	}
	
	$.ajax({
		type:"post",
		url:baseUrl + "/updateUserById",
		data:
		{
			"userId":analyseSearch("userId"),
			"username":username,
			"password":password,
			"gender":gender=="男"?"1":"0",
			"classname":classname,
			"type":(type == "老师" ? "2" : "1")
		},
		dataType:"json",
		success:function(data)
		{
			if(data["meta"]["status"] == "200")
			{
				window.location.assign("index.html?title=" + analyseSearch("title").substring(0, 7));
			}
			else 
			{
				$("#myModalBody").text("保存失败！");
				$("#myModal").modal("show");
			}
		},
		error:function()
		{
			$("#myModalBody").text("网络错误！");
			$("#myModal").modal("show");
		}
		
	});
	
	
}


function deleteTeacherOrStudent(userId)
{
	
	$.ajax({
		type:"post",
		url:baseUrl + "/deleteUserById",
		async:true,
		dataType:"json",
		data:
		{
			"userId":userId
		},
		success:function(data)
		{
			if(data["meta"]["status"] == 200)
			{
				window.location.reload();
			}
			else 
			{
				$("#myModalBody").text("删除失败！");
				$("#myModal").modal("show");
			}
		},
		error:function()
		{
			$("#myModalBody").text("网络错误！");
			$("#myModal").modal("show");
		}
	});
	
	
	
	
}
